package com.cdck.safe.dete.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdck.safe.dete.model.entity.CheckProject;
import com.cdck.safe.dete.mapper.CheckProjectMapper;
import com.cdck.safe.dete.model.query.BaseKeywordsQuery;
import com.cdck.safe.dete.service.CheckProjectService;
import com.cdck.safe.dete.service.SysDeptService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (CheckProject)表服务实现类
 *
 * @author Sakura
 * @since 2024-02-29 10:30:30
 */
@Service
@RequiredArgsConstructor
public class CheckProjectServiceImpl extends ServiceImpl<CheckProjectMapper, CheckProject> implements CheckProjectService {


    private  final SysDeptService sysDeptService;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public CheckProject getCheckProjectFormData(Long id) {
        return this.getById(id);
    }

    /**
     * 分页查询
     *
     * queryParams 筛选条件
     * @return 查询结果
     */
    @Override
    public IPage<CheckProject> queryByPage(BaseKeywordsQuery queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        String keywords = queryParams.getKeywords();
        Page<CheckProject> page = new Page<>(pageNum, pageSize);
         this.page(
                page,
                new LambdaQueryWrapper<CheckProject>().like(StrUtil.isNotBlank(keywords), CheckProject::getName, keywords)
        );
        page.getRecords().forEach(item->
            item.setDeptName(sysDeptService.getById(item.getDeptId()).getName())
        );
        return page;
     }

    /**
     * 新增数据
     *
     * @param checkProject 实例对象
     * @return 实例对象
     */
    @Override
    public boolean saveCheckProject(CheckProject checkProject) {
        checkProject.setCreateTime(LocalDateTime.now());
        checkProject.setUpdateTime(LocalDateTime.now());
       return this.save(checkProject);
    }

    /**
     * 修改数据
     *
     * @param checkProject 实例对象
     * @return 实例对象
     */
    @Override
    public boolean updateCheckProject(Long id, CheckProject checkProject) {
        checkProject.setUpdateTime(LocalDateTime.now());
        return this.updateById(checkProject);
    }

    /**
     * 通过主键删除数据
     *
     * @param idsStr ids集合
     * @return 是否成功
     */
    @Override
    public boolean deleteCheckProjects(String idsStr) {
         Assert.isTrue(StrUtil.isNotBlank(idsStr), "删除的数据为空");
        // 逻辑删除
        List<Long> ids = Arrays.stream(idsStr.split(","))
                .map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(ids);
    }

    /**
     * 查询所有套餐
     * @return 套餐
     */
    @Override
    public List<CheckProject> queryAllProject() {
        return this.list();
    }
}
